package com.study.mybatis.controller;

import com.study.mybatis.entity.StClass;
import com.study.mybatis.mapper.UserMapper;
import com.study.mybatis.service.StClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class MpperController {

    @Autowired
    private StClassService stClassService;

    @Autowired
    private UserMapper userMapper;
    @PostMapping("/update")
    public String fun(@RequestBody StClass stClass){
        try{
            stClassService.update(stClass);
            return "hello,琪哥！！！";
        }catch (DataAccessResourceFailureException e){
            return "当前数据正在被被人审批，请稍后重试";
        }
    }

    @GetMapping("/fun")
    public   List<StClass>  fun1(){
        try{
            List<StClass> stClasses = userMapper.selectAllUsers();
            System.out.println(stClasses);
            List<StClass> allClasses = stClassService.getAllClasses();
            return allClasses;
        }catch (DataAccessResourceFailureException e){
            return null;
        }
    }


    private static Object object = new Object();
    private static Runnable runnable = () -> {
        synchronized (object) {
            System.out.println(Thread.currentThread().getName() + "---");
            try{
                object.wait();
            }catch (Exception e){
            }
        }
    };
    public static void main(String[] args) {
        Thread thread = new Thread(runnable);
        Thread thread2 = new Thread(runnable);
        thread.start();
        thread2.start();
    }
}
